package com.tangsm.spring.boot.validation.handler;


import com.tangsm.spring.boot.validation.exception.BusinessException;
import com.tangsm.spring.boot.validation.domain.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常统一处理
 *
 * @author tangsm
 */
@Order(90)
@RestControllerAdvice
public class BusinessExceptionHandle {
    private static final Logger log = LoggerFactory.getLogger(BusinessExceptionHandle.class);

    /**
     * 自定义异常统一处理
     * @param e BusinessException异常
     * @return 响应数据
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException, 自定义异常统一返回, {}", e.getMessage());
        return Result.failure(e.getResultStatus());
    }

    /**
     * 全局异常统一处理
     * @param e Exception异常
     * @return 响应数据
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public Result<?> exceptionHandler(Exception e) {
        log.error("Exception，全局异常统一返回, {}", e.getMessage());
        return Result.failure();
    }
}
